package com.tally.server.service;

import com.tally.server.domain.Message;
import com.tally.server.domain.MessageExample;
import com.tally.server.dto.MessageDTO;
import com.tally.server.dto.PageDTO;
import com.tally.server.enums.MessageReadEnum;
import com.tally.server.mapper.MessageMapper;
import com.tally.server.utils.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Date;

@Service
public class MessageService {
    private static final Logger logger = LoggerFactory.getLogger(MessageService.class);

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private PostService postService;

    @Autowired
    private UserService userService;

    /**
     * 列表查询
     */
    public void list(PageDTO pageDTO, Integer userId) {
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getSize());
        MessageExample messageExample = new MessageExample();
        messageExample.createCriteria().andRecUserIdEqualTo(userId);
        messageExample.setOrderByClause("create_time desc");
        List<Message> messageList = messageMapper.selectByExample(messageExample);
        PageInfo<Message> pageInfo = new PageInfo<>(messageList);
        pageDTO.setTotal(pageInfo.getTotal());
        List<MessageDTO> messageDTOList = CopyUtil.copyList(messageList, MessageDTO.class);
        messageDTOList.forEach(messageDTO -> {
            messageDTO.setPost(postService.getByIdWithoutBlobs(messageDTO.getPostId()));
            messageDTO.setSendUser(userService.getById(messageDTO.getSendUserId()));
        });
        pageDTO.setList(messageDTOList);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    public void save(MessageDTO messageDTO) {
        Message message = CopyUtil.copy(messageDTO, Message.class);
        if (StringUtils.isEmpty(messageDTO.getId())) {
            this.insert(message);
        } else {
            this.update(message);
        }
    }

    /**
     * 新增
     */
    private void insert(Message message) {
        Date now = new Date();
        message.setCreateTime(now);
        message.setRead(MessageReadEnum.UNREAD.getCode());
        messageMapper.insert(message);
    }

    /**
     * 更新
     */
    private void update(Message message) {
        messageMapper.updateByPrimaryKey(message);
    }

    public void updateSelective(MessageDTO messageDTO) {
        Message message = CopyUtil.copy(messageDTO, Message.class);
        messageMapper.updateByPrimaryKeySelective(message);
    }

    public Long countUnread(Integer userId) {
        MessageExample messageExample = new MessageExample();
        messageExample.createCriteria().andRecUserIdEqualTo(userId).andReadEqualTo(MessageReadEnum.UNREAD.getCode());
        return messageMapper.countByExample(messageExample);
    }

    public void deleteByPostId(String id) {
        MessageExample messageExample = new MessageExample();
        messageExample.createCriteria().andPostIdEqualTo(id);
        int i = messageMapper.deleteByExample(messageExample);
        logger.info("已删除id为{}的{}条消息", id, i);
    }
}
